home *** CD-ROM | disk | FTP | other *** search
/ The 640 MEG Shareware Studio 2 / The 640 Meg Shareware Studio CD-ROM Volume II (Data Express)(1993).ISO / clang / c_course.zip / CHAP07.TXT < prev    next >
Text File  |  1989-12-30  |  18KB  |  389 lines

  1.                       Chapter 7 - Strings and Arrays
  2.  
  3.  
  4.                              WHAT IS A STRING?
  5.  
  6.              A  string is a group of characters,  usually letters of 
  7.         the  alphabet.   In order to format your printout in such  a 
  8.         way that it looks nice, has meaningful titles and names, and 
  9.         is  esthetically  pleasing to you and the people  using  the 
  10.         output of your program,  you need the ability to output text 
  11.         data.  Actually you have already been using strings, because 
  12.         the second program in this tutorial,  way back in Chapter 2, 
  13.         output a message that was handled internally as a string.  A 
  14.         complete  definition  is  a  series  of  "char"  type   data 
  15.         terminated by a NULL character, which is a zero. 
  16.  
  17.              When  C  is going to use a string of data in some  way, 
  18.         either  to compare it with another,  output it,  copy it  to 
  19.         another string,  or whatever, the functions are set up to do 
  20.         what they are called to do until a NULL, which is a zero, is 
  21.         detected. 
  22.  
  23.                              WHAT IS AN ARRAY?
  24.  
  25.              An array is a series of homogeneous pieces of data that 
  26.         are all identical in type, but the type can be quite complex 
  27.         as  we will see when we get to the chapter of this  tutorial 
  28.         discussing structures.  A string is simply a special case of 
  29.         an array. 
  30.  
  31.              The  best way to see these principles is by use  of  an 
  32.         example,  so  load  the program CHRSTRG.C and display it  on 
  33.         your monitor.   The first thing new is the line that defines 
  34.         a "char" type of data entity.  The square brackets define an 
  35.         array subscript in C, and in the case of the data definition 
  36.         statement,  the  5 in the brackets defines 5 data fields  of 
  37.         type  "char" all defined as the variable "name".   In the  C 
  38.         language,  all subscripts start at 0 and increase by 1  each 
  39.         step  up  to  the  maximum which in  this  case  is  4.   We 
  40.         therefore  have  5 "char" type variables  named,  "name[0]", 
  41.         "name[1]",  "name[2]",  "name[3]",  and "name[4]".  You must 
  42.         keep in mind that in C, the subscripts actually go from 0 to 
  43.         one   less  than  the  number  defined  in  the   definition 
  44.         statement. 
  45.  
  46.                          HOW DO WE USE THE STRING?
  47.  
  48.              The  variable  "name" is therefore a string  which  can 
  49.         hold up to 5 characters, but since we need room for the NULL 
  50.         character,  there are actually only four useful  characters.  
  51.         To  load  something  useful  into  the  string,  we  have  5 
  52.         statements, each of which assigns one alphabetical character 
  53.         to one of the string characters.  Finally, the last place in 
  54.         the string is filled with the numeral 0 as the end indicator 
  55.  
  56.  
  57.                                   Page 42
  58.  
  59.  
  60.  
  61.  
  62.  
  63.  
  64.  
  65.  
  66.  
  67.                       Chapter 7 - Strings and Arrays
  68.  
  69.  
  70.         and  the string is complete.   (A "define" would allow us to 
  71.         use "NULL" instead of a zero,  and this would add greatly to 
  72.         the  clarity of the program.  It would be very obvious  that 
  73.         this  was  a  NULL  and not simply a  zero  for  some  other 
  74.         purpose.) Now that we have the string,  we will simply print 
  75.         it out with some other string data in the output statement. 
  76.  
  77.              The %s is the output definition to output a string  and 
  78.         the  system  will output characters starting with the  first 
  79.         one in "name" until it comes to the NULL character,  and  it 
  80.         will quit.   Notice that in the "printf" statement, only the 
  81.         variable  name "name" needs to be given,  with no  subscript 
  82.         since  we  are  interested  in starting  at  the  beginning.  
  83.         (There  is  actually another reason that only  the  variable 
  84.         name  is  given without brackets.   The discussion  of  that 
  85.         topic will be given in the next chapter.)
  86.  
  87.                         OUTPUTTING PART OF A STRING
  88.  
  89.              The  next "printf" illustrates that we can  output  any 
  90.         single  character of the string by using the "%c" and naming 
  91.         the particular character of "name" we want by including  the 
  92.         subscript.   The last "printf" illustrates how we can output 
  93.         part  of the string by stating the starting point by using a 
  94.         subscript.   The & specifies the address of  "name[1]".   We 
  95.         will  study this in the next chapter but I thought you would 
  96.         benefit from a little glimpse ahead.
  97.  
  98.              This example may make you feel that strings are  rather 
  99.         cumbersome  to  use since you have to set up each  character 
  100.         one  at  a time.   That is an incorrect  conclusion  because 
  101.         strings  are  very easy to use as we will see  in  the  next 
  102.         example program. 
  103.  
  104.              Compile and run this program.
  105.  
  106.                           SOME STRING SUBROUTINES
  107.  
  108.              Load  the  example program STRINGS.C for an example  of 
  109.         some  ways  to use strings.  First we define  four  strings.  
  110.         Next  we  come  to a new function that you  will  find  very 
  111.         useful,  the "strcpy" function,  or string copy.   It copies 
  112.         from  one  string  to another until it  comes  to  the  NULL 
  113.         character.   It is easy to remember which one gets copied to 
  114.         which  if  you think of them like an  assignment  statement.  
  115.         Thus if you were to say, for example, "x = 23;", the data is 
  116.         copied  from  the  right entity to the  left  one.   In  the 
  117.         "strcpy"  function,  the data is also copied from the  right 
  118.         entity  to  the left,  so that after execution of the  first 
  119.         statement,  name1 will contain the string  "Rosalinda",  but 
  120.  
  121.  
  122.  
  123.                                   Page 43
  124.  
  125.  
  126.  
  127.  
  128.  
  129.  
  130.  
  131.  
  132.  
  133.                       Chapter 7 - Strings and Arrays
  134.  
  135.  
  136.         without  the double quotes,  they are the compiler's way  of 
  137.         knowing that you are defining a string.
  138.  
  139.              Likewise,  "Zeke"  is copied into "name2" by the second 
  140.         statement,  then the "title" is copied.   The title and both 
  141.         names are then printed out.   Note that it is not  necessary 
  142.         for  the  defined string to be exactly the same size as  the 
  143.         string it will be called upon to store,  only that it is  at 
  144.         least  as long as the string plus one more character for the 
  145.         NULL. 
  146.  
  147.                       ALPHABETICAL SORTING OF STRINGS
  148.  
  149.              The  next function we will look at is the  "strcmp"  or 
  150.         the  string  compare function.   It will return a 1  if  the 
  151.         first string is larger than the second, zero if they are the 
  152.         same  length  and have the same characters,  and -1  if  the 
  153.         first  string  is  smaller  than the  second.   One  of  the 
  154.         strings,  depending  on the result of the compare is  copied 
  155.         into   the   variable  "mixed",   and   the   largest   name 
  156.         alphabetically  is  printed  out.   It  should  come  as  no 
  157.         surprise   to   you   that  "Zeke"  wins   because   it   is 
  158.         alphabetically  larger,  length  doesn't  matter,  only  the 
  159.         alphabet.  It might be wise to mention that the result would 
  160.         also depend on whether the letters were upper or lower case.  
  161.         There are functions available with your C compiler to change 
  162.         the  case of a string to all upper or all lower case if  you 
  163.         desire.   These  will be used in an example program later in 
  164.         this tutorial.
  165.  
  166.                              COMBINING STRINGS
  167.  
  168.              The last four statements have another new feature,  the 
  169.         "strcat",  or string concatenation function.   This function 
  170.         simply  adds the characters from one string onto the end  of 
  171.         another string taking care to adjust the NULL so  everything 
  172.         is  still all right.   In this case,  "name1" is copied into 
  173.         "mixed",  then two blanks are concatenated to  "mixed",  and 
  174.         finally  "name2"  is concatenated to the  combination.   The 
  175.         result  is printed out with both names in the  one  variable 
  176.         "mixed". 
  177.  
  178.              Strings  are  not difficult and are  extremely  useful.  
  179.         You should spend some time getting familiar with them before 
  180.         proceeding on to the next topic.
  181.  
  182.              Compile  and run this program and observe  the  results 
  183.         for compliance with this definition.
  184.  
  185.  
  186.  
  187.  
  188.  
  189.                                   Page 44
  190.  
  191.  
  192.  
  193.  
  194.  
  195.  
  196.  
  197.  
  198.  
  199.                       Chapter 7 - Strings and Arrays
  200.  
  201.  
  202.                             AN ARRAY OF INTEGERS
  203.  
  204.              Load the file INTARRAY.C and display it on your monitor 
  205.         for  an  example of an array of integers.   Notice that  the 
  206.         array is defined in much the same way we defined an array of 
  207.         char  in  order to do the string manipulations in  the  last 
  208.         section.   We  have  12 integer variables to work  with  not 
  209.         counting the one named "index".   The names of the variables 
  210.         are "values[0]",  "values[1]", ... , and "values[11]".  Next 
  211.         we have a loop to assign nonsense, but well defined, data to 
  212.         each of the 12 variables,  then print all 12 out. You should 
  213.         have  no  trouble following this program,  but be  sure  you 
  214.         understand  it.   Compile and run it to see if it does  what 
  215.         you expect it to do.
  216.  
  217.                       AN ARRAY OF FLOATING POINT DATA
  218.  
  219.              Load  and display the program named BIGARRAY.C  for  an 
  220.         example  of  a program with an array of "float"  type  data.  
  221.         This  program has an extra feature to illustrate how strings 
  222.         can  be  initialized.    The  first  line  of  the   program 
  223.         illustrates to you how to initialize a string of characters.  
  224.         Notice  that the square brackets are empty leaving it up  to 
  225.         the  compiler  to count the characters and  allocate  enough 
  226.         space for our string.   Another string is initialized in the 
  227.         body  of the program but it must be declared "static"  here.  
  228.         This  prevents  it  from being allocated as  an  "automatic" 
  229.         variable and allows it to retain the string once the program 
  230.         is started.   There is nothing else new here,  the variables 
  231.         are  assigned  nonsense  data and the  results  of  all  the 
  232.         nonsense are printed out along with a header.   This program 
  233.         should also be easy for you to follow, so study it until you 
  234.         are  sure  of what it is doing before going on to  the  next 
  235.         topic.
  236.  
  237.                      GETTING DATA BACK FROM A FUNCTION
  238.  
  239.              Back  in chapter 5 when we studied functions,  I hinted 
  240.         to you that there was a way to get data back from a function 
  241.         by  using  an array,  and that is true.   Load  the  program 
  242.         PASSBACK.C for an example of doing that.   In this  program, 
  243.         we  define  an array of 20 variables  named  "matrix",  then 
  244.         assign  some nonsense data to the variables,  and print  out 
  245.         the  first five.   Then we call the function "dosome" taking 
  246.         along  the entire array by putting the name of the array  in 
  247.         the parentheses.
  248.  
  249.              The  function  "dosome" has a name in  its  parentheses 
  250.         also but it prefers to call the array "list".   The function 
  251.         needs  to be told that it is really getting an array  passed 
  252.         to  it and that the array is of type "int".   The  following 
  253.  
  254.  
  255.                                   Page 45
  256.  
  257.  
  258.  
  259.  
  260.  
  261.  
  262.  
  263.  
  264.  
  265.                       Chapter 7 - Strings and Arrays
  266.  
  267.  
  268.         line,  prior to the bracket which starts the  program,  does 
  269.         that  by  defining  "list" as an integer type  variable  and 
  270.         including the square brackets to indicate an array.   It  is 
  271.         not  necessary to tell the function how many elements are in 
  272.         the  array,  but you could if you so desired.   Generally  a 
  273.         function  works with an array until some end-of-data  marker 
  274.         is  found,  such  as  a NULL for a  string,  or  some  other 
  275.         previously  defined data or pattern.   Many  times,  another 
  276.         piece of data is passed to the function with a count of  how 
  277.  
  278.         many elements to work with.  In our present illustration, we 
  279.         will use a fixed number of elements to keep it simple.
  280.  
  281.              So far nothing is different from the previous functions 
  282.         we  have called except that we have passed more data  points 
  283.         to  the function this time than we ever have before,  having 
  284.         passed 20 integer values.  We print out the first 5 again to 
  285.         see if they did indeed get passed here.   Then we add ten to 
  286.         each of the elements and print out the new values.   Finally 
  287.         we return to the main program and print out the same 5  data 
  288.         points.   We  find that we have indeed modified the data  in 
  289.         the function,  and when we returned to the main program,  we 
  290.         brought  the changes back.   Compile and run this program to 
  291.         verify this conclusion.
  292.  
  293.                          ARRAYS PASS DATA BOTH WAYS
  294.  
  295.              We  stated during our study of functions that  when  we 
  296.         passed data to a function,  the system made a copy to use in 
  297.         the  function which was thrown away when we returned.   This 
  298.         is not the case with arrays.   The actual array is passed to 
  299.         the  function  and  the function can modify it  any  way  it 
  300.         wishes  to.    The  result  of  the  modifications  will  be 
  301.         available  back  in  the calling  program.   This  may  seem 
  302.         strange  to  you that arrays are  handled  differently  from 
  303.         single point data, but they are.  It really does make sense, 
  304.         but  you  will  have  to wait until we get  to  pointers  to 
  305.         understand it. 
  306.  
  307.                          A HINT AT A FUTURE LESSON
  308.  
  309.              Another way of getting data back from a function to the 
  310.         calling program is by using pointers which we will cover  in 
  311.         the  next chapter.   When we get there we will find that  an 
  312.         array  is in reality a pointer to a list of  values.   Don't 
  313.         let  that  worry  you now,  it will make sense when  we  get 
  314.         there.  In the meantime concentrate on arrays and understand 
  315.         the  basics  of  them because when we get to  the  study  of 
  316.         structures  we will be able to define some pretty  elaborate 
  317.         arrays.
  318.  
  319.  
  320.  
  321.                                   Page 46
  322.  
  323.  
  324.  
  325.  
  326.  
  327.  
  328.  
  329.  
  330.  
  331.                       Chapter 7 - Strings and Arrays
  332.  
  333.  
  334.                         MULTIPLY DIMENSIONED ARRAYS
  335.  
  336.              Load  and  display  the file named  MULTIARY.C  for  an 
  337.         example  of a program with doubly dimensioned  arrays.   The 
  338.         variable "big" is an 8 by 8 array that contains 8 times 8 or 
  339.         64 elements total.   The first element is  "big[0][0]",  and 
  340.         the last is "big[7][7]".  Another array named "huge" is also 
  341.         defined  which  is not square to illustrate that  the  array 
  342.         need  not  be square.   Both are filled up  with  data,  one 
  343.  
  344.         representing  a  multiplication  table and the  other  being 
  345.         formed into an addition table. 
  346.  
  347.              To illustrate that individual elements can be  modified 
  348.         at will,  one of the elements of "big" is assigned the value 
  349.         from one of the elements of "huge" after being multiplied by 
  350.         22.   Next "big[2][2]" is assigned the arbitrary value of 5, 
  351.         and  this  value  is  used for the subscripts  of  the  next 
  352.         assignment statement.   The third assignment statement is in 
  353.         reality  "big[5][5]  = 177" because each of  the  subscripts 
  354.         contain the value 5.   This is only done to illustrate  that 
  355.         any  valid expression can be used for a subscript.   It must 
  356.         only meet two conditions,  it must be an integer (although a 
  357.         "char"  will work just as well),  and it must be within  the 
  358.         range of the subscript it is being used for.
  359.  
  360.              The  entire matrix variable "big" is printed out  in  a 
  361.         square  form so you can check the values to see if they  did 
  362.         get set the way you expected them to.
  363.  
  364.         PROGRAMMING EXERCISES
  365.  
  366.         1.   Write  a  program with  three short  strings,  about  6 
  367.              characters each, and use "strcpy" to copy "one", "two", 
  368.              and  "three" into them.  Concatenate the three  strings 
  369.              into one string and print the result out 10 times.
  370.  
  371.         2.   Define  two  integer  arrays,  each 10  elements  long, 
  372.              called "array1" and "array2".  Using a loop,  put some 
  373.              kind  of  nonsense data in each and add them  term  for 
  374.              term  into  another 10 element  array  named  "arrays". 
  375.              Finally,  print  all  results in a table with an  index 
  376.              number.
  377.              1     2 +  10 =  12
  378.              2     4 +  20 =  24
  379.              3     6 +  30 =  36   etc.
  380.  
  381.              Hint; The print statement will be similar to;
  382.                 printf("%4d %4d + %4d = %4d\n",index,array1[index],
  383.                         array2[index],arrays[index]);
  384.  
  385.  
  386.  
  387.                                   Page 47
  388.  
  389.